<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Typeoption;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class TypeoptionController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('字段');
            $content->description('管理');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('字段');

            $content->body($this->form($id)->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加');
            $content->description('字段');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Typeoption::class, function (Grid $grid) {

            $grid->model()->OrderBy('order', 'asc');

            $grid->id('ID')->sortable();

            $grid->column('order', '显示顺序')->editable();
            $grid->column('parent_id', '分组')->display(function ($type) {
                $types = Typeoption::getType();
                return $types[$type];
            });
            $grid->column('title', '	名称')->editable();
            $grid->column('identifier', '	变量名');
            $grid->column('type', '类型')->display(function ($type) {
                $types = Typeoption::getFieldType();

                return $types[$type];
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($edit = false)
    {
        return Admin::form(Typeoption::class, function (Form $form) use ($edit) {

            $data = $form->model()->find($edit);

            $parent_id_f = $form->select('parent_id', '所属类型')->options(Typeoption::getType())->setWidth(2);

            $form->text('title', '名称')->setWidth(2)->rules('required');
            $form->text('identifier', '变量名')->setWidth(2)->rules('required');


            $type_f = $form->select('type', '类型')->options(Typeoption::getFieldType())->setWidth(2)->help('类型保存后不可修改');


            $form->textarea('description', '简短描述')->setWidth(2);
            $form->text('unit', '单位')->setWidth(2);

            if ($edit) {
                $form->embeds('rules', '字段信息', function ($form) use ($data) {
                    $this->_setField($form, $data->type);

                });
                $parent_id_f->readOnly();
                $type_f->readOnly();
            }
            $form->saving(function (Form $form) {
                $form->embeds('rules', '字段信息', function ($forms) use ($form) {
                    $this->_setField($forms, $form->type);
                });
            });
        });
    }

    /**
     * @param Form $form
     * @param $type
     * @return mixed
     */
    protected function _setField($form, $type)
    {
        switch ($type) {
            case 'number':
                $form->text('maxnum', '最大值');
                $form->text('minnum', '最小值');
                $form->text('defaultvalue', '默认值');
                break;
            case 'text':
                $form->text('maxlength', '最大值');
                $form->text('minlength', '最小值');
                $form->text('defaultvalue', '默认值');
                break;
            case 'radio':
                $form->tags('radio', '可选内容');
                break;
            case 'checkbox':
                $form->tags('radio', '可选内容');
                break;
            case 'select':
                $form->tags('select', '可选内容');
                break;
            case 'textarea':
                $form->text('maxlength', '最大值');
                $form->text('minlength', '最小值');
                $form->text('defaultvalue', '默认值');
                break;
            case 'model':
                $form->text('mode', '关联模型');
                $form->text('forkey', '关联KEY');
                $form->text('showkey', '显示值');
                $form->text('where', '附加查询条件');
                break;
        }
        return $form;
    }
}
